package com.zhiyou100.review.day021.homework.homework3;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @packageName: UploadFile
 * @className: com.zhiyou100.review.day021.homework.homework3
 * @Description: TODO 上传文件 客户端
 * @author: yang
 * @date: 7/2/20
 */
public class UploadFile {
    private static final String FILE_NAME = "/Users/yang/Downloads/test/send/a.txt";
    private static final String IP = "127.0.0.1";
    private static final int PORT = 10086;

    public static void main(String[] args) throws Exception {
        File file = new File(FILE_NAME);
        // 创建文件


        Socket socket = new Socket(IP, PORT);
        // 创建 socket

        OutputStream out = socket.getOutputStream();
        // 获取输出流
        out.write(getFileContent(FILE_NAME).getBytes(StandardCharsets.UTF_8));
        // 写入

        socket.shutdownOutput();
        // 关闭输出流


        socket.close();
        // 关流

    }

    /**
     * @Description: TODO 获取 String 中的数字
     * @name: getDigest
     * @param: [into]
     * @return: java.lang.String
     * @date: 7/2/20 19:44
     * @author: yang
     */
    public static String getDigest(String into) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < into.length(); i++) {
            char charAt = into.charAt(i);
            if (Character.isDigit(charAt)) {
                stringBuilder.append(charAt);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * @Description: TODO 获取 String 中的 字母
     * @name: getLetter
     * @param: [into]
     * @return: java.lang.String
     * @date: 7/2/20 19:46
     * @author: yang
     */
    public static String getLetter(String into) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < into.length(); i++) {
            char charAt = into.charAt(i);
            if (Character.isUpperCase(charAt) || Character.isLowerCase(charAt)) {
                stringBuilder.append(charAt);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * @Description: TODO 获取其他字符
     * @name: getAuthor
     * @param: [into]
     * @return: java.lang.String
     * @date: 7/2/20 19:48
     * @author: yang
     */
    public static String getOther(String into) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < into.length(); i++) {
            char charAt = into.charAt(i);
            boolean flag = (!Character.isDigit(charAt)) && (!Character.isUpperCase(charAt)) && (!Character.isLowerCase(charAt));
            if (flag) {
                stringBuilder.append(charAt);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * @Description: TODO 读取一个文件的内容，返回一个 String
     * @name: getFileContent
     * @param: [fileNames]
     * @return: java.lang.String
     * @date: 7/2/20 20:12
     * @author: yang
     */
    public static String getFileContent(String fileNames) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(fileNames));
        // 创建 bufferReader 和 文件 关联
        StringBuilder stringBuilder = new StringBuilder();

        while (true) {
            String s = reader.readLine();
            if (s == null) {
                break;
            }
            stringBuilder.append(s);
        }
        reader.close();
        return stringBuilder.toString();
    }
}
